﻿// -----------------------------------------------------------------------
// <copyright file="ProjectReader.cs" company="Raop">
// Project reader.
// </copyright>
// -----------------------------------------------------------------------

namespace Raop.Library.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Diagnostics.Contracts;
    using System.IO;
    using System.Text.RegularExpressions;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class SolutionReader
    {
        public string DirectoryName { get; private set; }

        public string SolutionFile { get; private set; }

        public SolutionReader(string solutionFile)
        {
            Contract.Requires(solutionFile != null);
            if (!File.Exists(solutionFile))
            {
                throw new FileNotFoundException();
            }

            this.SolutionFile = solutionFile;

            this.DirectoryName = new FileInfo(this.SolutionFile).DirectoryName;
        }

        public List<string> GetProjects()
        {
            Contract.Requires(this.SolutionFile != null);
            Contract.Ensures(Contract.Result<List<string>>() != null);

            var regex = new Regex(@"""([^""]+\.csproj)""");

            var projects = new List<string>();

            var input = File.ReadAllText(this.SolutionFile);

            foreach (Match item in regex.Matches(input))
            {
                projects.Add(Path.Combine(this.DirectoryName, item.Groups[1].Value));
            }

            return projects;
        }
    }
}
